/*
 * @Author: Colin Dai
 * @Date: 2022-05-10 13:51:44
 * @LastEditors: Colin Dai
 * @LastEditTime: 2022-05-10 14:47:48
 * @FilePath: /jy-uos/src/softbus/communication/udp/source/udp_bus_manager.c
 * @Description: 
 * dailianzheng@163.com
 * Copyright (c) 2022 by Colin.Dai, All Rights Reserved. 
 */
#include "udp_bus_manager.h"
#include "discovery_error.h"
#include "log.h"

BaseListener g_baseLister = {0};

//函数前置声明
int OnUdpConnectEvent(int fd, const char *ip);
int OnUdpDataEvent(int fd);

/**
 * @brief 启动udp发现总线
 * @param {char} *ip
 * @return {*}
 */
int Start_udp_Bus(const char *ip)
{
   
    g_baseLister.onConnectEvent = OnConnectEvent;
    g_baseLister.onDataEvent = OnDataEvent;

    /*TODO 需要处理
    int authPort = StartListener(&g_baseLister, info->deviceIp);
    if (authPort < 0) {
        SOFTBUS_PRINT("[AUTH] StartBus StartListener fail\n");
        return ERROR_FAIL;
    }
    */
    //info->devicePort = 0;

    /*

    int sessionPort = StartSession(info->deviceIp);
    if (sessionPort < 0) {
        SOFTBUS_PRINT("[AUTH] StartBus StartSession fail\n");
        StopListener();
        return ERROR_FAIL;
    }
    */
  

    SOFTBUS_PRINT("[UDP_BUS] StartBus ok\n");
    return 0;
}

/**
 * @brief 停止总线
 * @param {*}
 * @return {*}
 */
int Stop_udp_Bus(void)
{
    

    /*
    //FIXME 临时屏蔽
    //AuthMngDeInit();
    StopListener();

    int ret = RemoveTcpSessionMgr();
    if (ret != 0) {
        return ERROR_FAIL;
    }

    DeviceInfo *info = GetCommonDeviceInfo();
    if (info == NULL) {
        return ERROR_FAIL;
    }
    info->devicePort = -1;    */

    SOFTBUS_PRINT("[UDP_BUS] Stop bus ok\n");
    return ERROR_SUCCESS;
}

/** 
 * @brief UDP接口有连接返回
 * @param {int} fd
 * @param {char} *ip
 * @return {*}
 */
int OnUdpConnectEvent(int fd, const char *ip)
{   
    ProcessConnectEvent(fd, ip);  
    return 0;
}

/**
 * @brief UDP接口有数据返回
 * @param {int} fd
 * @return {*}
 */
int OnUdpDataEvent(int fd)
{  
    ProcessDataEvent(fd);
    return 0;
}
